package dao;

import java.util.Date;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * Teacher entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see dao.Teacher
 * @author MyEclipse Persistence Tools
 */

public class TeacherDAO extends BaseHibernateDAO {
    private static final Logger log = LoggerFactory.getLogger(TeacherDAO.class);
    // property constants
    public static final String NAME = "name";
    public static final String SEX = "sex";
    public static final String DESCRIPTION = "description";

    public boolean save(Teacher transientInstance) {
	log.debug("saving Teacher instance and saving User");
	Session session = getSession();
	Transaction transaction = session.beginTransaction();
	try {
	    int type = 2;
	    if ("1".equals(transientInstance.getRank())) {
		type = 3;
	    }
	    User user = new User(transientInstance.getTeacherid(), "teacher",
		    "teacher123", type);
	    session.save(transientInstance);
	    session.save(user);
	    transaction.commit();
	    log.debug("save successful");
	    return true;
	} catch (RuntimeException re) {
	    transaction.rollback();
	    log.error("save failed", re);
	    throw re;
	}
    }

    public boolean delete(Teacher persistentInstance) {
	return delete(log, "deleting Teacher instance", persistentInstance);
    }

    public Teacher findById(java.lang.String id) {
	log.debug("getting Teacher instance with id: " + id);
	try {
	    Teacher instance = (Teacher) getSession().get("dao.Teacher", id);
	    return instance;
	} catch (RuntimeException re) {
	    log.error("get failed", re);
	    throw re;
	}
    }

    public List<Teacher> findByExample(Teacher instance) {
	log.debug("finding Teacher instance by example");
	try {
	    List<Teacher> results = (List<Teacher>) getSession()
		    .createCriteria("dao.Teacher").add(create(instance)).list();
	    log.debug("find by example successful, result size: "
		    + results.size());
	    return results;
	} catch (RuntimeException re) {
	    log.error("find by example failed", re);
	    throw re;
	}
    }

    public List findByProperty(String propertyName, Object value) {
	log.debug("finding Teacher instance with property: " + propertyName
		+ ", value: " + value);
	try {
	    String queryString = "from Teacher as model where model."
		    + propertyName + "= ?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, value);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find by property name failed", re);
	    throw re;
	}
    }

    public List<Teacher> findByName(Object name) {
	return findByProperty(NAME, name);
    }

    public List<Teacher> findBySex(Object sex) {
	return findByProperty(SEX, sex);
    }

    public List<Teacher> findByDescription(Object description) {
	return findByProperty(DESCRIPTION, description);
    }

    public List findAll() {
	log.debug("finding all Teacher instances");
	try {
	    String queryString = "from Teacher";
	    Query queryObject = getSession().createQuery(queryString);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	}
    }

    public int getTeacherCounts() {
	try {
	    String queryString = "select count(teacherid) from Teacher";
	    Query queryObject = getSession().createQuery(queryString);
	    return ((Number) queryObject.uniqueResult()).intValue();// Long向上转换为Number,再转换为Integer
	} catch (RuntimeException re) {
	    throw re;
	}
    }

    public List<Teacher> findTeachers(int pageSize, int pageNum) {
	try {
	    String queryString = "from Teacher";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setFirstResult((pageNum - 1) * pageSize);
	    queryObject.setMaxResults(pageSize);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    throw re;
	}
    }

    public List<Teacher> findTeachersByGrade(String grade) {
	try {
	    String queryString = "from Teacher where courseid like ?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setString(0, grade+"__");
	    return queryObject.list();
	} catch (RuntimeException re) {
	    throw re;
	}
    }

    public Teacher merge(Teacher detachedInstance) {
	log.debug("merging Teacher instance");
	try {
	    Teacher result = (Teacher) getSession().merge(detachedInstance);
	    log.debug("merge successful");
	    return result;
	} catch (RuntimeException re) {
	    log.error("merge failed", re);
	    throw re;
	}
    }

    public boolean update(Teacher instance) {
	return update(log, "updating Teacher instance", instance);
    }

    public void attachClean(Teacher instance) {
	log.debug("attaching clean Teacher instance");
	try {
	    getSession().lock(instance, LockMode.NONE);
	    log.debug("attach successful");
	} catch (RuntimeException re) {
	    log.error("attach failed", re);
	    throw re;
	}
    }
    
    public static void main(String[] args){
	TeacherDAO teacherDAO = new TeacherDAO();
	System.out.println(teacherDAO.findTeachersByGrade("01").size());
    }
}